package util;

import java.util.Random;

public class Array {
    // 生成随机一维数组，范围 [ 0 - 100)
    public static int[] creatArrayOneDimensional(int arrayLength){
        Random r = new Random();
        int[] Array = new int[arrayLength];
        for (int i = 0; i < arrayLength; i++) {
            Array[i] = r.nextInt(100);
        }
        return Array;
    }
    // 生成随机一维数组，自定义范围 [ start - end)
    public static int[] creatArrayOneDimensional(int arrayLength,int start,int end){
        Random r = new Random();
        int[] Array = new int[arrayLength];
        for (int i = 0; i < arrayLength; i++) { // 5 -10
            Array[i] = r.nextInt(start) + (end -start);
        }
        return Array;
    }

    // 生成随机二维数组，范围 [ 0 - 100)
    public static int[][] creatArrayTwoDimensional(int row, int cloum){
        Random r = new Random();
        int[][] Array = new int[row][cloum];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < cloum; j++) {
                Array[i][j] = r.nextInt(100);
            }
        }
        return Array;
    }
    // 生成随机二维数组，自定义范围 [ start - end)
    public static int[][] creatArrayTwoDimensional(int row, int cloum, int start, int end){
        Random r = new Random();
        int[][] Array = new int[row][cloum];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < cloum; j++) {
                Array[i][j] = r.nextInt(start) + (end -start);
            }
        }
        return Array;
    }
    // 生成随机二维数组，初始化为：initial
    public static int[][] creatArrayTwoDimensional(int row, int cloum, int initial) {
        int[][] Array = new int[row][cloum];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < cloum; j++) {
                Array[i][j] = initial;
            }
        }
        return Array;
    }
    // 快速排序 一维(全排)
    public static int[] quickSortArrayOneDimensional(int[] array){
        int pivot = array[0];
        int start = 0;
        int end = array.length-1;
        int i = start;
        int j = end;
        while (i<j) {
            while ((i<j)&&(array[j]>pivot)) {
                j--;
            }
            while ((i<j)&&(array[i]<pivot)) {
                i++;
            }
            if ((array[i]==array[j])&&(i<j)) {
                i++;
            } else {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
        if (i-1>start)
            array=quickSortArrayOneDimensional(array,start,i-1);
        if (j+1<end)
            array=quickSortArrayOneDimensional(array,j+1,end);
        return array;
    }

    // 快速排序 一维（指定区间）
    public static int[] quickSortArrayOneDimensional(int[] array,int start, int end){
        int pivot = array[start];
        int i = start;
        int j = end;
        while (i<j) {
            while ((i<j)&&(array[j]>pivot)) {
                j--;
            }
            while ((i<j)&&(array[i]<pivot)) {
                i++;
            }
            if ((array[i]==array[j])&&(i<j)) {
                i++;
            } else {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
        if (i-1>start)
            array=quickSortArrayOneDimensional(array,start,i-1);
        if (j+1<end)
            array=quickSortArrayOneDimensional(array,j+1,end);
        return array;
    }
    // 快速排序 二维
    public static int[][] quickSortArrayTwoDimensional(int[][] array){
        int row = array.length;
        int cloum = array[0].length;
        int[] arrayOneDimensional = new int[row*cloum];

        for (int i = 0,k=0; i < row; i++) {
            for (int j = 0; j < cloum; j++) {
                arrayOneDimensional[k++] = array[i][j];
            }
        }
        quickSortArrayOneDimensional(arrayOneDimensional,0,row*cloum-1);
        for (int i = 0; i < arrayOneDimensional.length; i++) {
            System.out.println(arrayOneDimensional[i]+"\t");
        }
        int i = 0, j = 0;
        for (int k = 0; k < arrayOneDimensional.length; k++) {
            array[i][j++] = arrayOneDimensional[k];
            if((j)%cloum == 0){
                j=0;
                i++;
            }
        }
        return array;
    }

    // 输出一维数组
    public static void showArrayOneDimensional(int[] array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+"\t");
        }
        System.out.println();
        System.out.println();
    }
    // 输出一维数组
    public static void showArrayOneDimensional(int[] array, int rowNumber){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+"\t");
            if ((i+1)%rowNumber == 0) System.out.println();
        }
        System.out.println();
    }
    // 输出二维数组
    public static void showArrayTwoDimensional(int[][] array){
        int row = array.length;
        int cloum = array[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < cloum; j++) {
                System.out.print(array[i][j]+"\t");
            }
            System.out.println();
        }
        System.out.println();
    }
}
